<template>
  <div class="oe-new-question" v-loading="loading">
    <top-tip :courseTitle="courseTitle"></top-tip>
    <div class="oe-new-contain">
      <div class="test-1" style="height: 500px;display: inline-block;overflow-y: auto;vertical-align: top;width: 211px;">
        <tree-select v-if="treeShow" :linkage="linkage" ref="tree" class="table-class" style="border-right: 1px solid #ccc;margin-right: -1px;" :know="know" :list="treeList"></tree-select>
      </div>
      <div class="table-class" style="width: calc(100% - 211px);border-left: 1px solid #ccc;margin-left: -1px;">
        <span style="color: #f00; font-size: 18px; font-weight: bold; margin-right: 5px">*</span>
        <a-select
          v-model="quesType"
          :disabled="disabled"
          style="width: 150px"
          placeholder="请选择题型"
          @change="setQuesType"
        >
          <a-select-option v-for="(item, index) in typelist" :value="item.id" :index="index" :key="item.id">{{
            item.text
          }}</a-select-option>
        </a-select>
        <span style="padding: 0px 5px; font-size: 18px; color: #ccc">|</span>
        <span style="color: #f00; font-size: 18px; font-weight: bold; margin-right: 5px">*</span>
        <a-select v-model="degree" style="width: 150px" placeholder="请选择难度">
          <a-select-option v-for="(item, index) in defficulty" :value="item.id" :index="index" :key="item.id">{{
            item.text
          }}</a-select-option>
        </a-select>
        <!-- <span style="padding: 0px 5px; font-size: 18px; color: #ccc">|</span>
        <a-select mode="multiple" v-model="know" style="min-width: 150px" placeholder="请选择知识点">
          <a-select-option v-for="(item, index) in knowList" :value="item.id" :index="index" :key="item.id">{{
            item.text
          }}</a-select-option>
        </a-select> -->
        <span v-if="quesType == 6" style="color: red; margin-left: 20px"
          >*问答题在发布考试时，可以设置是否允许学生上传附件</span
        >
        <div class="oe-border"></div>
        <div class="oe-set-question" v-if="questionShow">
          <radioCheck
            ref="child"
            v-if="quesType == 1 || quesType == 2"
            :quesData="quesData"
            :quesType="quesType"
          ></radioCheck>
          <trueFalse ref="child" v-if="quesType == 3" :quesData="quesData" :quesType="quesType"></trueFalse>
          <exercise ref="child" v-if="quesType == 4" :quesData="quesData" :quesType="quesType"></exercise>
          <exercises ref="child" v-if="quesType == 5" :quesData="quesData" :quesType="quesType"></exercises>
          <questionAnswer ref="child" v-if="quesType == 6" :quesData="quesData" :quesType="quesType"></questionAnswer>
          <matching ref="child" v-if="quesType == 7" :quesData="quesData" :quesType="quesType"></matching>
          <questionChild
            ref="child"
            v-if="quesType == 8 || quesType == 9 || quesType == 11 || quesType == 12 || quesType == 13"
            :quesData="quesData"
            :quesType="quesType"
          ></questionChild>
        </div>
        <div class="oe-btn-content" v-if="quesType">
          <a-button type="primary" @click="setSubject(1)">保存</a-button>
          <a-button type="primary" v-if="this.quizid == ''" @click="setSubject(2)">保存新建下一题</a-button>
          <a-button @click="getBack">取消</a-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import topTip from '@/components/top-tip/index.vue';
import radioCheck from '@/components/addTi/radioCheck.vue';
import trueFalse from '@/components/addTi/trueFalse.vue';
import exercise from '@/components/addTi/exercise.vue';
import exercises from '@/components/addTi/exercise1.vue';
import questionAnswer from '@/components/addTi/questionAnswer.vue';
import matching from '@/components/addTi/matching.vue';
import questionChild from '@/components/addTi/questionChild.vue';
import GetOperationList from '@/lib/mixin/methods.js';
import treeSelect from '@/components/treeSelect/index.vue';
export default {
  components: {
    topTip,
    radioCheck,
    trueFalse,
    exercise,
    exercises,
    questionAnswer,
    matching,
    questionChild,
    treeSelect,
  },
  mixins: [GetOperationList],
  data() {
    return {
      treeList: [],
      loading: false,
      linkage: false,
      disabled: false,
      quesType: undefined,
      typelist: [
        { id: 1, text: '单选题(客观)' },
        { id: 2, text: '多选题(客观)' },
        { id: 3, text: '判断(客观)' },
        { id: 4, text: '填空题(客观)' },
        { id: 5, text: '填空题(主观)' },
        { id: 6, text: '问答题(主观)' },
        { id: 7, text: '匹配题(客观)' },
        { id: 8, text: '阅读理解' },
        { id: 9, text: '完形填空' },
        { id: 11, text: '视听题(客观)' },
      ],
      typelistchild: [
        { id: 1, text: '单选题(客观)' },
        { id: 2, text: '多选题(客观)' },
        { id: 3, text: '判断(客观)' },
      ],
      degree: 1,
      defficulty: [
        { id: 1, text: '非常简单' },
        { id: 2, text: '简单' },
        { id: 3, text: '一般' },
        { id: 4, text: '困难' },
        { id: 5, text: '非常困难' },
      ],
      know: undefined,
      knowList: [],
      questionId: '',
      datajson: [],
      subdata: [],
      answers: [],
      quesData: {},
      saveType: 1,
      questionShow: true,
      courseTitle: '',
      treeShow: false
    };
  },
  props: {
    addid: {
      type: String,
      default: '',
    },
    quizid: {
      type: String,
      default: '',
    },
  },
  mounted() {
    this.getKnowledgeList();
    this.getOperation();
    this.getAllKnowledgeTreeList();
    if (this.quizid != '') {
      this.loading = true;
      this.previewQuestion();
      this.disabled = true;
    }
  },
  methods: {
    // 获取知识点
    getAllKnowledgeTreeList() {
      this.$http
        .post('/api/design/knowledgetree/getAllKnowledgeTreeList', {
          courseId: this.addid, //分类Id
        })
        .then((res) => {
          if (res.code > 0) {
            this.treeList = res.knowledgeList;
            if (this.quizid == '') {
              this.treeShow = true;
            }
          } else {
            this.$message.error(rst.msg);
          }
        });
    },
    getOperation() {
      this.loading = true;
      this.GetOperationList('courseId', this.addid).then((rst) => {
        this.loading = false;
        if (rst.code === 1) {
          this.courseTitle = rst.courseTitle;
        } else {
          this.$message.error(rst.msg);
        }
      });
    },
    previewQuestion() {
      this.quesData = {};
      this.$http
        .post('/api/design/questionbank/previewQuestion', {
          questionId: this.quizid,
        })
        .then((rst) => {
          this.loading = false;
          if (rst.code > 0) {
            if (rst.question.questionType == 12) {
              this.quesType = 8;
            } else if (rst.question.questionType == 13) {
              this.quesType = 9;
            } else {
              this.quesType = rst.question.questionType;
            }
            this.degree = rst.question.difficultLevel;
            if (rst.question.knowledgeIds) {
              this.know = rst.question.knowledgeIds.split(',');
            }
            this.treeShow = true;
            if (rst.question.questionType == 1) {
              // 单选题
              var radio = {
                id: rst.question.Id,
                type: 1,
                isShowQuesType: true,
                content: rst.question.allTitle,
                quesFileList: rst.question.fileList,
                options: [],
                answer: Number(rst.question.answer),
                analysis: rst.question.resultAnalysis,
              };
              for (let i = 0; i < rst.question.optionList.length; i++) {
                let lis = { content: rst.question.optionList[i].Content };
                radio.options.push(lis);
              }
              this.quesData = radio;
            } else if (rst.question.questionType == 2) {
              // 多选题
              var check = {
                id: rst.question.Id,
                type: 2,
                isShowQuesType: true,
                content: rst.question.allTitle,
                quesFileList: rst.question.fileList,
                options: [],
                answer: rst.question.answer.split(',').map(Number),
                analysis: rst.question.resultAnalysis,
              };
              for (let i = 0; i < rst.question.optionList.length; i++) {
                let lis = { content: rst.question.optionList[i].Content };
                check.options.push(lis);
              }
              this.quesData = check;
            } else if (rst.question.questionType == 3) {
              // 判断题
              var judge = {
                id: rst.question.Id,
                type: 3,
                content: rst.question.allTitle,
                isShowQuesType: true,
                quesFileList: rst.question.fileList,
                options: [
                  {
                    value: 1,
                    content: '正确',
                  },
                  {
                    value: 0,
                    content: '错误',
                  },
                ],
                answer: Number(rst.question.answer),
                analysis: rst.question.resultAnalysis,
              };
              this.quesData = judge;
            } else if (rst.question.questionType == 4) {
              // // 填空(客观)
              var exercise = {
                id: rst.question.Id,
                type: 4,
                content: rst.question.allTitle,
                quesFileList: rst.question.fileList,
                options: [],
                answer: [],
                isOrderMatching: rst.question.IsOrderMatching,
                analysis: rst.question.resultAnalysis,
              };
              var num = [];
              for (let i = 0; i < rst.answerList.length; i++) {
                var key = rst.answerList[i].SortOrder;
                if (num.length < key + 1) {
                  let list = {
                    Order: [
                      {
                        SortOrder: rst.answerList[i].SortOrder,
                        Content: rst.answerList[i].Content,
                        Percent: rst.answerList[i].Percent,
                        CaseSensitive: rst.answerList[i].CaseSensitive,
                        ScoreType: rst.answerList[i].ScoreType,
                      },
                    ],
                  };
                  num.push(list);
                } else {
                  let lis = {
                    SortOrder: rst.answerList[i].SortOrder,
                    Content: rst.answerList[i].Content,
                    Percent: rst.answerList[i].Percent,
                    CaseSensitive: rst.answerList[i].CaseSensitive,
                    ScoreType: rst.answerList[i].ScoreType,
                  };
                  num[key].Order.push(lis);
                }
              }
              exercise.answer = num;
              this.quesData = exercise;
            } else if (rst.question.questionType == 5) {
              // 填空(主观)
              var exercises = {
                id: rst.question.Id,
                type: 5,
                content: rst.question.allTitle,
                quesFileList: rst.question.fileList,
                options: [],
                answer: [],
                analysis: rst.question.resultAnalysis,
              };
              for (let i = 0; i < rst.question.optionList.length; i++) {
                let lis = {
                  SortOrder: rst.question.optionList[i].SortOrder,
                  Content: rst.question.optionList[i].Content,
                };
                exercises.answer.push(lis);
              }
              this.quesData = exercises;
            } else if (rst.question.questionType == 6) {
              // 问答题
              var quesAnswer = {
                id: rst.question.Id,
                type: 6,
                content: rst.question.allTitle,
                quesFileList: rst.question.fileList,
                options: [],
                answer: rst.question.answer,
                analysis: rst.question.resultAnalysis,
                // standard: []
              };
              this.quesData = quesAnswer;
            } else if (rst.question.questionType == 7) {
              // 匹配题
              var matching = {
                id: rst.question.Id,
                type: 7,
                content: rst.question.allTitle,
                quesFileList: rst.question.fileList,
                options: {
                  s: [],
                  m: [],
                },
                answer: [],
                analysis: rst.question.resultAnalysis,
              };
              for (let i = 0; i < rst.question.optionList.length; i++) {
                let lis = {
                  content: rst.question.optionList[i].OptionContent,
                  optionSelectContent: Number(rst.question.optionList[i].OptionSelectContent),
                };
                matching.options.s.push(lis);
              }
              for (let i = 0; i < rst.question.questionAnswer.length; i++) {
                let lis = {
                  content: rst.question.questionAnswer[i].OptionAnswerContent,
                };
                matching.options.m.push(lis);
              }
              this.quesData = matching;
            } else if (
              rst.question.questionType == 8 ||
              rst.question.questionType == 9 ||
              rst.question.questionType == 11 ||
              rst.question.questionType == 12 ||
              rst.question.questionType == 13
            ) {
              var child = {
                id: rst.question.Id,
                type: rst.question.questionType,
                content: rst.question.allTitle,
                quesFileList: rst.question.fileList,
                options: [],
                answer: [],
                analysis: rst.question.resultAnalysis,
              };
              if (child.type == 12) {
                child.type = 8;
              } else if (child.type == 13) {
                child.type = 9;
              }
              let list = rst.subQuestionList;
              for (let i = 0; i < list.length; i++) {
                if (list[i].subQuestionType == 1 || list[i].subQuestionType == 2) {
                  var radioLis = {
                    id: list[i].subQuestionId,
                    type: list[i].subQuestionType,
                    content: list[i].subQuestionTitle,
                    quesFileList: list[i].quesFileList,
                    isShowQuesType: true,
                    options: [],
                    answer: [],
                    analysis: '',
                  };
                  if (list[i].subQuestionType == 1) {
                    radioLis.answer = Number(list[i].subAnswer);
                  } else {
                    radioLis.answer = list[i].subAnswer.split(',').map(Number);
                  }
                  for (let key = 0; key < list[i].subOptionList.length; key++) {
                    let subOptionLis = { content: '' };
                    subOptionLis.content = list[i].subOptionList[key].Content;
                    radioLis.options.push(subOptionLis);
                  }
                  child.options.push(radioLis);
                } else if (list[i].subQuestionType == 3) {
                  var trueFalseLis = {
                    id: list[i].subQuestionId,
                    type: 3,
                    content: list[i].subQuestionTitle,
                    quesFileList: list[i].quesFileList,
                    isShowQuesType: true,
                    options: [
                      {
                        value: 1,
                        content: '正确',
                      },
                      {
                        value: 0,
                        content: '错误',
                      },
                    ],
                    answer: Number(list[i].subAnswer),
                    analysis: '',
                  };
                  child.options.push(trueFalseLis);
                } else if (list[i].subQuestionType == 6) {
                  var answerLis = {
                    id: list[i].subQuestionId,
                    type: 6,
                    content: list[i].subQuestionTitle,
                    quesFileList: list[i].quesFileList,
                    options: [],
                    answer: list[i].subAnswer,
                    analysis: '',
                  };
                  child.options.push(answerLis);
                }
              }
              this.quesData = child;
            }
          } else {
            this.$message.warning('未找到此题目');
          }
        });
    },
    //获取知识点
    getKnowledgeList() {
      this.$http
        .post('/api/design/question/getKnowList', {
          courseId: this.addid,
        })
        .then((rst) => {
          this.knowList = rst;
        });
    },
    setQuesType(value) {
      var that = this;
      var radio = {
        id: '',
        type: 1,
        isShowQuesType: true,
        content: '',
        quesFileList: [],
        options: [
          {
            content: '',
          },
          {
            content: '',
          },
          {
            content: '',
          },
          {
            content: '',
          },
        ],
        answer: '',
        analysis: '',
      };
      var check = {
        id: '',
        type: 2,
        isShowQuesType: true,
        content: '',
        quesFileList: [],
        options: [
          {
            content: '',
          },
          {
            content: '',
          },
          {
            content: '',
          },
          {
            content: '',
          },
        ],
        answer: [],
        analysis: '',
      };
      var judge = {
        id: '',
        type: 3,
        content: '',
        isShowQuesType: true,
        quesFileList: [],
        options: [
          {
            value: 1,
            content: '正确',
          },
          {
            value: 0,
            content: '错误',
          },
        ],
        answer: '',
        analysis: '',
      };
      var exercise = {
        id: '',
        type: 4,
        content: '',
        quesFileList: [],
        options: [],
        answer: [],
        isOrderMatching: true,
        analysis: '',
      };
      var exercises = {
        id: '',
        type: 5,
        content: '',
        quesFileList: [],
        options: [],
        answer: [],
        analysis: '',
      };
      var quesAnswer = {
        id: '',
        type: 6,
        content: '',
        quesFileList: [],
        options: [],
        answer: [],
        analysis: '',
        // standard: []
      };
      var matching = {
        id: '',
        type: 7,
        content: '',
        quesFileList: [],
        options: {
          s: [
            {
              content: '',
              optionSelectContent: undefined,
            },
            {
              content: '',
              optionSelectContent: undefined,
            },
          ],
          m: [
            {
              content: '',
            },
            {
              content: '',
            },
          ],
        },
        answer: [],
        analysis: '',
      };
      var child = {
        id: '',
        type: 8,
        content: '',
        quesFileList: [],
        options: [
          {
            id: '',
            type: 1,
            content: '',
            quesFileList: [],
            isShowQuesType: true,
            options: [
              {
                content: '',
              },
              {
                content: '',
              },
              {
                content: '',
              },
              {
                content: '',
              },
            ],
            answer: '',
            analysis: '',
          },
          {
            id: '',
            type: 2,
            content: '',
            quesFileList: [],
            isShowQuesType: true,
            options: [
              {
                content: '',
              },
              {
                content: '',
              },
              {
                content: '',
              },
              {
                content: '',
              },
            ],
            answer: [],
            analysis: '',
          },
          {
            id: '',
            type: 3,
            content: '',
            quesFileList: [],
            isShowQuesType: true,
            options: [
              {
                value: 1,
                content: '正确',
              },
              {
                value: 0,
                content: '错误',
              },
            ],
            answer: '',
            analysis: '',
          },
          {
            id: '',
            type: 6,
            content: '',
            quesFileList: [],
            options: [],
            answer: [],
            analysis: '',
            // standard: []
          },
        ],
        answer: [],
        analysis: '',
      };
      var child1 = {
        id: '',
        type: 8,
        content: '',
        quesFileList: [],
        options: [
          {
            id: '',
            type: 1,
            content: '',
            quesFileList: [],
            isShowQuesType: true,
            options: [
              {
                content: '',
              },
              {
                content: '',
              },
              {
                content: '',
              },
              {
                content: '',
              },
            ],
            answer: '',
            analysis: '',
          },
          {
            id: '',
            type: 2,
            content: '',
            quesFileList: [],
            isShowQuesType: true,
            options: [
              {
                content: '',
              },
              {
                content: '',
              },
              {
                content: '',
              },
              {
                content: '',
              },
            ],
            answer: [],
            analysis: '',
          },
          {
            id: '',
            type: 3,
            content: '',
            quesFileList: [],
            isShowQuesType: true,
            options: [
              {
                value: 1,
                content: '正确',
              },
              {
                value: 0,
                content: '错误',
              },
            ],
            answer: '',
            analysis: '',
          },
        ],
        answer: [],
        analysis: '',
      };
      var audioVisual = {
        id: '',
        type: 11,
        content: '',
        quesFileList: [],
        options: [
          {
            id: '',
            type: 1,
            content: '',
            quesFileList: [],
            isShowQuesType: true,
            options: [
              {
                content: '',
              },
              {
                content: '',
              },
              {
                content: '',
              },
              {
                content: '',
              },
            ],
            answer: '',
            analysis: '',
          },
        ],
        answer: [],
        analysis: '',
      };
      that.quesData = {};
      if (that.quesType == 1) {
        that.quesData = radio;
      } else if (that.quesType == 2) {
        that.quesData = check;
      } else if (that.quesType == 3) {
        that.quesData = judge;
      } else if (that.quesType == 4) {
        that.quesData = exercise;
      } else if (that.quesType == 5) {
        that.quesData = exercises;
      } else if (that.quesType == 6) {
        that.quesData = quesAnswer;
      } else if (that.quesType == 7) {
        that.quesData = matching;
      } else if (that.quesType == 8 || that.quesType == 9) {
        child.type = that.quesType;
        that.quesData = child;
      } else if (that.quesType == 11) {
        that.quesData = audioVisual;
      }
    },
    // 检验 题干是否填写
    setSubject(type) {
      this.saveType = type;
      this.datajson = [];
      if (this.quesType == 8 || this.quesType == 9 || this.quesType == 11) {
        if (this.quesData.content == '') {
          return this.$message.warning('请填写大题题干');
        } else {
          var childContent = true;
          if (this.quesData.options.length > 0) {
            for (let i = 0; i < this.quesData.options.length; i++) {
              if (this.quesData.options[i].content == '') {
                childContent = false;
              }
            }
          } else {
            childContent = false;
            return this.$message.warning('该题型必须有一题子题目');
          }
          if (childContent) {
            this.setOptions();
          } else {
            return this.$message.warning('请填写子题题干');
          }
        }
      } else {
        if (this.quesData.content == '') {
          return this.$message.warning('请填写题干');
        } else {
          this.setOptions();
        }
      }
    },
    // 检验 选项是否填写
    setOptions() {
      if (this.quesType == 1 || this.quesType == 2) {
        let optionShow = true;
        for (let i = 0; i < this.quesData.options.length; i++) {
          if (this.quesData.options[i].content == '') {
            optionShow = false;
          }
        }
        if (optionShow) {
          var dataArr = [];
          var optionLis = {
            SortOrder: 0,
            Content: '',
            isAnswer: false,
          };
          if (this.quesType == 1) {
            for (let i = 0; i < this.quesData.options.length; i++) {
              optionLis = {
                SortOrder: i,
                Content: this.quesData.options[i].content,
                isAnswer: i == this.quesData.answer,
              };
              dataArr.push(optionLis);
            }
          } else if (this.quesType == 2) {
            for (let i = 0; i < this.quesData.options.length; i++) {
              let isAnswer = false;
              for (let key = 0; key < this.quesData.answer.length; key++) {
                if (i == this.quesData.answer[key]) {
                  isAnswer = true;
                }
              }
              optionLis = {
                SortOrder: i,
                Content: this.quesData.options[i].content,
                isAnswer: isAnswer,
              };
              dataArr.push(optionLis);
            }
          }
          this.datajson = dataArr;
          this.setAnswer();
        } else {
          this.$message.warning('请填写选项');
        }
      } else if (this.quesType == 4) {
        var OrderShow = true;
        if (this.quesData.answer.length > 0) {
          for (let i = 0; i < this.quesData.answer.length; i++) {
            for (let key = 0; key < this.quesData.answer[i].Order.length; key++) {
              this.quesData.answer[i].Order[key].Content = this.quesData.answer[i].Order[key].Content.replace(
                /(^\s*)|(\s*)$/g,
                ''
              );
              if (this.quesData.answer[i].Order[key].Content == '') {
                OrderShow = false;
              }
            }
          }
        } else {
          OrderShow = false;
          this.$message.warning('该题型必须有一填空');
        }
        if (OrderShow) {
          this.setAnswer();
        } else {
          this.$message.warning('请填写填空题的答案');
        }
      } else if (this.quesType == 7) {
        let optionShow = true;
        for (let i = 0; i < this.quesData.options.s.length; i++) {
          if (this.quesData.options.s[i].content == '') {
            optionShow = false;
          }
          if (this.quesData.options.s[i].optionSelectContent == undefined) {
            optionShow = false;
          }
        }
        for (let i = 0; i < this.quesData.options.m.length; i++) {
          if (this.quesData.options.m[i].content == '') {
            optionShow = false;
          }
        }
        if (optionShow) {
          for (let i = 0; i < this.quesData.options.s.length; i++) {
            let lis = {
              //{"SortOrder":0,"":"<p>A</p>\n","":2}
              SortOrder: i,
              optionContent: this.quesData.options.s[i].content,
              optionSelectContent: this.quesData.options.s[i].optionSelectContent,
            };
            this.datajson.push(lis);
          }
          for (let i = 0; i < this.quesData.options.m.length; i++) {
            let lim = {
              SortOrder: i,
              optionAnswerContent: this.quesData.options.m[i].content,
            };
            this.quesData.answer.push(lim);
          }
          this.setAnswer();
        } else {
          this.$message.warning('请填写选项');
        }
      } else if (this.quesType == 8 || this.quesType == 9 || this.quesType == 11) {
        let optionShow = true;
        for (let i = 0; i < this.quesData.options.length; i++) {
          let type = this.quesData.options[i].type;
          let child = this.quesData.options[i];
          if (type == 1 || type == 2) {
            for (let key = 0; key < child.options.length; key++) {
              if (child.options[key].content == '') {
                optionShow = false;
              }
            }
          }
        }
        if (optionShow) {
          this.setAnswer();
        } else {
          this.$message.warning('请填写选项');
        }
      } else {
        this.setAnswer();
      }
    },
    // 检验 答案是否设置
    setAnswer() {
      if (this.quesType == 1 || this.quesType == 3) {
        if (this.quesData.answer === '') {
          return this.$message.warning('请选择正确答案');
        } else {
          this.saveQues();
        }
      } else if (this.quesType == 2) {
        if (this.quesData.answer.length > 0) {
          this.quesData.answer = this.quesData.answer.join(',');
          this.saveQues();
        } else {
          return this.$message.warning('请选择正确答案');
        }
      } else if (this.quesType == 4) {
        // 客观填空 上一步验证是否有答案 此步数据结构重组
        var answerArr = [];
        for (let i = 0; i < this.quesData.answer.length; i++) {
          for (let key = 0; key < this.quesData.answer[i].Order.length; key++) {
            answerArr.push(this.quesData.answer[i].Order[key]);
          }
        }
        this.keyContent = answerArr;
        for (let i = 0; i < this.keyContent.length; i++) {
          let lis = {
            SortOrder: this.keyContent[i].SortOrder,
            Content: this.keyContent[i].Content,
            Percent: this.keyContent[i].Percent,
            ScoreType: this.keyContent[i].ScoreType,
            CaseSensitive: this.keyContent[i].CaseSensitive,
            isAnswer: true,
          };
          this.datajson.push(lis);
        }
        this.saveQues();
      } else if (this.quesType == 5) {
        var answerShow = true;
        if (this.quesData.answer.length > 0) {
          for (let i = 0; i < this.quesData.answer.length; i++) {
            if (this.quesData.answer[i].Content == '') {
              answerShow = false;
            }
          }
        } else {
          answerShow = false;
          return this.$message.warning('该题型必须有一题子题目');
        }
        if (answerShow) {
          for (let i = 0; i < this.quesData.answer.length; i++) {
            let lis = {
              SortOrder: i,
              Content: this.quesData.answer[i].Content,
              isAnswer: true,
            };
            this.datajson.push(lis);
          }
          this.saveQues();
        } else {
          return this.$message.warning('请填写正确答案');
        }
      } else if (this.quesType == 6) {
        if (this.quesData.answer == '') {
          return this.$message.warning('请填写正确答案');
        } else {
          this.saveQues();
        }
      } else if (this.quesType == 7) {
        this.saveQues();
      } else if (
        this.quesType == 8 ||
        this.quesType == 9 ||
        this.quesType == 11 ||
        this.quesType == 12 ||
        this.quesType == 13
      ) {
        let answerShow = true;
        for (let i = 0; i < this.quesData.options.length; i++) {
          let type = this.quesData.options[i].type;
          let child = this.quesData.options[i];

          if (type == 1 || type == 3 || type == 6) {
            if (child.answer === '') {
              answerShow = false;
            }
          } else if (type == 2) {
            if (child.answer.length <= 0) {
              answerShow = false;
            }
          }
        }
        if (answerShow) {
          this.saveQues();
        } else {
          return this.$message.warning('请选择正确答案');
        }
      } else {
        return this.$message.warning('没有此类型');
      }
    },
    getBack() {
      this.$router.back();
    },
    saveQues() {
      this.loading = true;
      var answers = '';
      var isOrderMatching = '';
      var keyContent = '';
      if (this.quesType == 4) {
        keyContent = this.keyContent;
      } else {
        keyContent = this.quesData.answer;
      }
      if (this.quesType == 6) {
        answers = this.quesData.answer;
      } else if (this.quesType == 4) {
        answers = JSON.stringify(this.quesData.answer);
        isOrderMatching = this.quesData.isOrderMatching;
        var keyType = -1;
        keyContent = '';
        for (let key = 0; key < this.quesData.answer.length; key++) {
          if (keyType < 0) {
            keyType = this.quesData.answer[key].SortOrder;
            keyContent = this.quesData.answer[key].Content;
          } else {
            if (keyType == this.quesData.answer[key].SortOrder) {
              keyContent = keyContent + ';' + this.quesData.answer[key].Content;
            } else {
              keyType = this.quesData.answer[key].SortOrder;
              keyContent = keyContent + ',' + this.quesData.answer[key].Content;
            }
          }
        }
        // this.quesData.answer = keyContent;
      } else if (
        this.quesType == 8 ||
        this.quesType == 9 ||
        this.quesType == 11 ||
        this.quesType == 12 ||
        this.quesType == 13
      ) {
        this.subdata = [];
        for (let i = 0; i < this.quesData.options.length; i++) {
          var lis = {};
          let type = this.quesData.options[i].type;
          let child = this.quesData.options[i];
          if (type == 1) {
            lis = {
              Id: child.id,
              Title: child.content,
              DataJson: [],
              answer: String(child.answer),
              SubQuestionType: child.type,
              quesFileList: child.quesFileList,
            };
            for (let key = 0; key < child.options.length; key++) {
              let li = {
                SortOrder: key,
                Content: child.options[key].content,
                isAnswer: child.answer == key,
              };
              lis.DataJson.push(li);
            }
            lis.DataJson = JSON.stringify(lis.DataJson);
          } else if (type == 2) {
            lis = {
              Id: child.id,
              Title: child.content,
              DataJson: [],
              answer: child.answer,
              SubQuestionType: child.type,
              quesFileList: child.quesFileList,
            };
            for (let key = 0; key < child.options.length; key++) {
              let isAnswer = false;
              for (let index = 0; index < child.answer.length; index++) {
                if (child.answer[index] == key) {
                  isAnswer = true;
                }
              }
              let li = {
                SortOrder: key,
                Content: child.options[key].content,
                isAnswer: isAnswer,
              };
              lis.DataJson.push(li);
            }
            lis.answer = lis.answer.join(',');
            lis.DataJson = JSON.stringify(lis.DataJson);
          } else if (type == 3) {
            lis = {
              Id: child.id,
              Title: child.content,
              DataJson: '[]',
              answer: String(child.answer),
              SubQuestionType: child.type,
              quesFileList: child.quesFileList,
            };
          } else if (type == 6) {
            lis = {
              Id: child.id,
              Title: child.content,
              DataJson: '[]',
              answer: child.answer,
              SubQuestionType: child.type,
              quesFileList: child.quesFileList,
            };
          }
          this.subdata.push(lis);
        }
      } else if (this.quesType == 2) {
        answers = '';
      } else {
        answers = JSON.stringify(this.quesData.answer);
      }
      var knowledgeId = this.$refs.tree.ids.join(',');
      console.log(knowledgeId);
      this.questionShow = true;
      this.$http
        .post('/api/design/questionbank/saveQues', {
          questionId: this.quizid, //题目Id
          courseId: this.addid, //课程Id
          knowledgeIds: knowledgeId, //知识点Id
          questionTitle: this.quesData.content, //题目标题
          jsonUrl: JSON.stringify(this.quesData.quesFileList), //题目附件
          difficultyLevel: this.degree, //题目难度1.非常简单，2.简单，3.一般，4.困难，5.非常困难
          questionType: this.quesType, //题目类型 1.单选题(客观),2.多选题(客观),3.判断(客观),5.填空题(主观),6.问答题(主观),7.匹配题(客观),8.阅读理解(客观),9.完形填空(客观),11.视听题(客观)
          selectContent: keyContent, //题目答案(选择题)
          answers: answers, //题目答案 this.paperList[this.typeid].answer.join(',')
          dataJson: JSON.stringify(this.datajson), //题目数据json
          analysis: this.quesData.analysis, //解析
          isOrderMatching: isOrderMatching, //严格按照标准答案顺序
          keyWord: '', //关键字(只针对填空，问答题进行批阅，多个关键字英文逗号隔开)
          subQuestions: JSON.stringify(this.subdata), //子题目
        })
        .then((res) => {
          this.loading = false;
          this.questionShow = true;
          if (res.code == 1) {
            this.quesType = undefined;
            if (this.saveType == 1) {
              this.quesData = {};
              this.$router.go(-1);
            } else {
              this.setQuesType();
            }
            this.$message.success(res.msg);
            this.know = undefined;
          } else {
            this.$message.error(res.msg);
          }
        });
    },
  },
};
</script>

<style>
.table-class{
  display: inline-table;
}
</style>